using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using DebugCompanion.Engine;

namespace DebugCompanion
{
	partial class ProjectSelectionDlg : Form
	{
		ProjectSelectionDlg()
		{
			InitializeComponent();
		}

		private void _configureBtn_Click(object sender, EventArgs e)
		{
			if (_itemsList.SelectedItem != null)
				ConfigureProject((ProjectInfo)_itemsList.SelectedItem);
		}

		public static new void Show()
		{
			ProjectSelectionDlg dlg = new ProjectSelectionDlg();
			List<ProjectInfo> projects = VsUtil.GetProjectsList();

			foreach (ProjectInfo info in projects)
			{
				if (info.ProjectType == ProjectInfo.WebPseudoProjectGuid
					|| info.IsExecutable
					|| info.ProjectType == ProjectInfo.WebProjectGuid)
					dlg._itemsList.Items.Add(info);
			}

			dlg.ShowDialog();
		}

		private void _itemsList_DoubleClick(object sender, EventArgs e)
		{
			MouseEventArgs args = (MouseEventArgs)e;
			int pos = _itemsList.IndexFromPoint(args.X, args.Y);
			if (pos != ListBox.NoMatches)
			{
				ConfigureProject((ProjectInfo)_itemsList.Items[pos]);
			}
		}

		void ConfigureProject(ProjectInfo info)
		{
			try
			{
				ProjectSettingsDlg.Show(info);
			}
			catch (Exception exc)
			{
				MessageBox.Show(exc.Message);
			}
		}

		private void _itemsList_SelectedIndexChanged(object sender, EventArgs e)
		{
			_configureBtn.Enabled = (_itemsList.SelectedItem != null);
		}
	}
}